<?php

namespace App\Wechat\Remind\Controllers;

use App\Exceptions\ApiException;
use App\Exceptions\Error;
use App\Wechat\Remind\Library\Response;
use App\Wechat\Remind\Logics\Remind;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class RemindController extends Controller
{
	public function index(Request $request)
	{
		$page_no = $request->get('pageNo', 1);
		$page_size = $request->get('pageSize', 10);
		if (!is_numeric($page_no) || !is_numeric($page_size)){
			throw new ApiException('参数错误', Error::PARAMS_ERROR);
		}
		$result = (new Remind())->listRemind($page_no, $page_size);
		return Response::json($result);
    }

	public function show($id)
	{
		$result = (new Remind())->showRemind($id);
		return Response::json($result);
    }

	public function store(Request $request)
	{
		$validator = Validator::make($request->all(), [
			'title' => 'required|max:255',
			'content' => 'required|max:800',
			'date' => 'required|date_format:Y-m-d',
			'time' => 'required|date_format:H:i',
			'wx_remind' => 'required|in:0,1',
			'tel_remind' => 'required|in:0,1',
		]);
		if ($validator->fails()){
			throw new ApiException('参数错误，请确认', Error::PARAMS_ERROR);
		}
		$result = (new Remind())->createRemind($request->all());
		return Response::json($result);
    }

	public function edit(Request $request, $id)
	{
		$validator = Validator::make($request->all(), [
			'title' => 'required|max:255',
			'content' => 'required|max:800',
			'date' => 'required|date_format:Y-m-d',
			'time' => 'required|date_format:H:i',
			'wx_remind' => 'required|in:0,1',
			'tel_remind' => 'required|in:0,1',
		]);
		if ($validator->fails()){
			throw new ApiException('参数错误，请确认', Error::PARAMS_ERROR);
		}
		$result = (new Remind())->editRemind($id, $request->all());
		return Response::json($result);
    }

	public function delete($id)
	{
		$result = (new Remind())->deleteRemind($id);
		return Response::json($result);
    }

	public function countRemind()
	{
		$result = (new Remind())->countRemind();
		return Response::json($result);
	}
}
